<?php


namespace app\admin\controller\goods;


use app\common\controller\Adminbase;
use think\Db;

class Goods extends Adminbase
{
    protected $searchFields = 'name';
    protected $goods_categoty;
    protected function initialize()
    {
        parent::initialize();
        $goods_categoty = Db::name('goods_categoty')
            ->where('is_del', '<>', 1)
            ->order('sort desc')
            ->select();
        $this->goods_categoty=$goods_categoty;
        $this->assign('goodsCategoty', $goods_categoty);
    }

    public function index()
    {
        //商品类型 商品名称 所需积分 排序 库存 兑换数量  操作：删除 编辑 种类不更改
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = Db::name('goods')
                ->order(array('create_time'=>'desc','id' => 'desc'))
                ->where($where)
                ->where('is_del', '<>', 1)
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['goods_cate2'] = Db::name('goods_categoty')
                    ->find($v['goods_cate']);
            }
            $total = Db::name('goods')
                ->where($where)
                ->where('is_del', '<>', 1)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);

            return json($result);
        }
        foreach ($this->goods_categoty as $k=>$v){
            $goods_categoty2[$v['id']]=$v['name'];
        }
        $this->assign('goods_categoty2',$goods_categoty2);
        return $this->fetch();
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'goods_cate' => $data['goods_cate'],
                'points' => $data['points'],
                'sort'=>$data['sort'],
                'create_time'=>time(),
                'inventory'=>$data['inventory'],
                'salenum'=>$data['salenum']
            );
            //1优惠卷 2陪诊服务 3医疗用品
            //0陪诊 1医疗用品 2优惠卷
            if ($data['goods_cate'] == 0) {
                $insert_data['object_id'] = $data['pz_object_id'];
                $server_info=Db::name('server')->find($data['pz_object_id']);
                $insert_data['name'] = $server_info['name'];
            }

            if ($data['goods_cate'] == 2) {
                $insert_data['object_id'] = $data['object_id'];
                $coupons_info=Db::name('coupons')->find($data['object_id']);
                $insert_data['name'] = $coupons_info['name'];
            }

            if ($data['goods_cate'] == 1) {
                $insert_data['object_id'] = 0;
                $insert_data['name'] = $data['name'];
                $insert_data['img'] = $data['img'];
                $insert_data['desc'] = $data['desc'];
                $insert_data['content'] = $data['content'];
            }
            $stmt = Db::name('goods')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        //商品分类
        $goods_cate_list = Db::name('goods_categoty')
            ->where([
                ['is_del', '=', 0],
            ])->order('sort desc')
            ->select();
        //获取优惠卷列表 未删除 上线
        //获取陪诊服务列表
        $coupons_list = Db::name('coupons')
            ->where([
                'is_del' => 0,
                'status' => 1,
                'cate_id'=>6
            ])->select();
        $server_list=Db::name('server')->where([
            'is_del' => 0,
            'status' => 1
        ])->select();
        $this->assign([
                'goods_cate_list' => $goods_cate_list,
                'coupons_list'=>$coupons_list,
                'server_list'=>$server_list
            ]
        );
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 推荐
     */
    public function recommend()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['recommend' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('goods')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $insert_data = array(
                'id' => $data['id'],
                'points' => $data['points'],
                'sort'=>$data['sort'],
                'create_time'=>time(),
                'inventory'=>$data['inventory'],
                'salenum'=>$data['salenum']
            );
            //<!--//0陪诊 1医疗用品 2优惠卷-->
            //1优惠卷 2陪诊服务 3医疗用品
            if ($data['goods_cate'] == 0) {
                $insert_data['object_id'] = $data['pz_object_id'];
                $server_info=Db::name('server')->find($data['pz_object_id']);
                $insert_data['name'] = $server_info['name'];
            }
            if ($data['goods_cate'] == 2) {
                $insert_data['object_id'] = $data['object_id'];
                $coupons_info=Db::name('coupons')->find($data['object_id']);
                $insert_data['name'] = $coupons_info['name'];
            }
            if ($data['goods_cate'] == 1) {
                $insert_data['object_id'] = 0;
                $insert_data['name'] = $data['name'];
                $insert_data['img'] = $data['img'];
                $insert_data['desc'] = $data['desc'];
                $insert_data['content'] = $data['content'];
            }
            $stmt = Db::name('goods')->update($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $id = $this->request->param('id/d', 0);
            $data = Db::name('goods')->find($id);
            //获取陪诊服务列表
            $coupons_list = Db::name('coupons')
                ->where([
                    'is_del' => 0,
                    'status' => 1,
                    'cate_id'=>6
                ])->select();
            $server_list=Db::name('server')->where([
                'is_del' => 0,
                'status' => 1
            ])->select();
            $this->assign([
                    'coupons_list'=>$coupons_list,
                    'server_list'=>$server_list,
                    'data'=>$data
                ]
            );
            $this->assign('data', $data);
            return $this->fetch('goods/goods/edit');
        }
    }
}